<?php


namespace App\Http\Controllers\App;


use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use App\Repositories\Gashapon\DrawRecordRepository;
use App\Repositories\Gashapon\LotteryRebuildRepository;
use App\Repositories\Gashapon\LotteryRepository;
use App\Repositories\Gashapon\PrizeRepository;
use App\Repositories\Gashapon\RankRepository;
use App\Repositories\Gashapon\SettingRepository;
use App\Repositories\Gashapon\UserRepository;
use App\Resource\Gashapon\Balance\Resource as BalanceResource;
use App\Resource\Gashapon\Setting\Resource as SettingResource;
use App\Resource\Gashapon\Prize\Resource as PrizeResource;
use App\Resource\Gashapon\Rank\LuckyResource;
use App\Resource\Gashapon\Draw\Resource as DrawResource;
use App\Response\Response;
use Illuminate\Http\Request;

class GashaponController extends BaseController
{

    public function draw(Request $request)
    {
        $userId = (int)UID;
        $count = (int)($request->input('count'));
        if($count <= 0){
            throw new BasicException(-1, '非法请求');
        }
        // $draw = LotteryRepository::getInstance()->draw($userId, $count);
        $draw = LotteryRebuildRepository::getInstance()->draw($userId, $count);
        $models = DrawRecordRepository::getInstance()->get(['draw_id' => $draw->id])->sortByDesc('prize_price');
        return Response::sendData(DrawResource::collection($models));
    }

    public function balance(Request $request)
    {
        $userId = (int)UID;
        $balance = UserRepository::getInstance()->info($userId);
        $model = BalanceResource::make($balance);
        return Response::sendData($model);
    }

    public function setting(Request $request)
    {
        $setting = SettingRepository::getInstance()->one([]);
        $model = SettingResource::make($setting);
        return Response::sendData($model);
    }

    public function rank(Request $request)
    {
        $userId = (int)UID;
        $perPage = (int) $request->query('per_page', 10);
        $date = $request->query('date', '');
        $models = RankRepository::getInstance()->luckyRank($userId, $date, $perPage);
        return Response::sendData(LuckyResource::make($models));
    }

    public function prizes(Request $request)
    {
        $models = PrizeRepository::getInstance()->show();
        return Response::sendData(PrizeResource::collection($models));
    }

    public function swap(Request $request)
    {
        $userId = (int)UID;
        $count = (int)($request->input('count'));
        if($count <= 0){
            throw new BasicException(-1, '非法请求');
        }
        UserRepository::getInstance()->swap($userId, $count);
        return Response::sendData([]);
    }


}